//
//  UserAccesser.m
//  MSJD
//
//  Created by xiangbo on 2018/9/24.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "UserAccesser.h"


@interface UserAccesser()

@property (nonatomic, strong) MSUser *account;
@property (nonatomic, copy) NSString *accountFilePath;//保存账号的文件路径

@end

@implementation UserAccesser

+ (instancetype)sharedInstance{
    static UserAccesser *accesser = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        accesser =[[UserAccesser alloc] init];
    });
    return accesser;
}

+ (BOOL)deleteArchiverWithPath:(NSString *)path{
    return [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

//用户账号路径
- (NSString *)accountFilePath {
    
    if (!_accountFilePath) {
        NSString *documenPath = [MSSafeObject stringInfo:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
        NSString *filePath = [documenPath stringByAppendingPathComponent:@"account"];
        BOOL bExist =  [[NSFileManager defaultManager] fileExistsAtPath:filePath];
        if (!bExist) {
            [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];
        }
        _accountFilePath = [filePath stringByAppendingPathComponent:@"account.archiver"];
        
    }
    return _accountFilePath;
}

///保存账号信息到本地，只有在登录成功后，才允许保存。
+ (BOOL)saveAccountToLocal:(MSUser *)accout
{
    [UserAccesser deleteArchiverWithPath:[UserAccesser sharedInstance].accountFilePath];
    [UserAccesser sharedInstance].account = accout;
    
    NSString *path = [UserAccesser sharedInstance].accountFilePath;
    BOOL bSuccess = [NSKeyedArchiver archiveRootObject:accout toFile:path];
    NSLog(@"path:%@", path);
    if (bSuccess) {
        NSLog(@"saveAccountToLocal-bSuccess:%d-BOOL userName = %@",bSuccess,accout.username);
    }
    return bSuccess;
}

///删除本地的账号信息
+ (void)deleteAccount {
    
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    NSString * path = [UserAccesser sharedInstance].accountFilePath;
    if ([defaultManager isDeletableFileAtPath:path]) {
        [UserAccesser deleteArchiverWithPath:path];
    }
    [UserAccesser sharedInstance].account = nil;
}

///返回当前账号信息 ModelAccout对象
+ (MSUser *)loadAccount {
    return [UserAccesser sharedInstance].account;
}


///当前是否有登录账号 YES表示有，NO表示没
+ (BOOL)hadDefaultAccount {
    
    if ([UserAccesser sharedInstance].account) {
        return YES;
    }
    return NO;
}

- (MSUser *)account {
    if (!_account) {
        NSString *filePath = self.accountFilePath;
        
        @try {
            [NSKeyedUnarchiver setClass:[MSUser class] forClassName:@"MSUser"];
            MSUser *account = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
            _account = account;
        } @catch (NSException *err) {
            NSLog(@"出现异常 %@",err);
            _account = nil;
        }
    }
    return _account;
}

+ (NSString *)userId{
    MSUser *user = [self loadAccount];
    return user.userId ? user.userId.stringValue : @"";
}

+(BOOL)isLogin{
    return [self loadAccount] ? YES : NO;
}

/**
 ⽤用户位置 纬度
 */
+ (CGFloat)latitudeValue {
    return [MSLocationManager shareInstance].currentPlace.location.coordinate.latitude;
}

/**
 用户位置 经度
 */
+ (CGFloat)longitudeValue {
    return [MSLocationManager shareInstance].currentPlace.location.coordinate.longitude;
}

@end
